//
//  videoViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "videoViewController.h"
#import <AVFoundation/AVFoundation.h>

static NSString *const Ncell = @"Ncell";

@interface videoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic, retain)NSMutableArray *myArray;
@property(nonatomic, assign)NSInteger *i;
@property(nonatomic, assign)BOOL isUpLoading;
@property(nonatomic, assign)NSInteger nextPage;


@property(nonatomic, retain)AVPlayer *videoPlayer;
@property(nonatomic, retain)AVPlayerLayer *layer1;
@property(nonatomic, retain)UIView *myView;

@end

@implementation videoViewController

-(void)dealloc{
    [_myTable release];
    [_myArray release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor colorWithRed:210/256.0 green:210/256.0 blue:210/256.0 alpha:1];
    self.myArray = [NSMutableArray array];
    
   
    
    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self.myTable registerClass:[videoTableViewCell class] forCellReuseIdentifier:Ncell];
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    self.isUpLoading = NO;
    
    [self addHeader];
    [self addFootder];
    
    //刚进界面就刷新
    [self.myTable.mj_header beginRefreshing];
    
    [self.myTable release];
    // Do any additional setup after loading the view.
}
#pragma make 上拉
-(void)addHeader{
    
    __block videoViewController *vc = self;//block中不能用self
    //上拉刷新
    self.myTable.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        vc.nextPage += 20;
        
        vc.isUpLoading = YES;
        [vc getData:vc.nextPage ];
    }];
    
}
#pragma make 下拉
-(void)addFootder{
    __block videoViewController *vc = self;
    //下拉刷新
    self.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        vc.nextPage = 0;//记录页码
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getData:vc.nextPage ];//请求数据
        
    }];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    videoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Ncell forIndexPath:indexPath];
    videoModel *vMod = [self.myArray objectAtIndex: indexPath.row];
    cell.model = vMod;

    cell.selectionStyle = NO;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height/5*2;
}

#pragma make 取数据
-(void)getData:(NSInteger)nextPage{
    
    
    //刷新之后页数重新设置为第一页
    
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/home/%ld-20.html",nextPage];
    
    [ZYNetworkHandle GetDataWithURL:url dic:nil success:^(id responseObject) {
        
        NSMutableArray *videoList = [responseObject objectForKey:@"videoList"];
        
        if (self.isUpLoading == NO) {
            [self.myArray removeAllObjects];
        }
        
        for (NSDictionary *dic in videoList) {
            
            videoModel *vMo = [[videoModel alloc] init];
            [vMo setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:vMo];
            
            [vMo release];
        }
        
        [self.myTable.mj_footer endRefreshing];
        [self.myTable.mj_header endRefreshing];
        
        
        [self.myTable reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSURL *url = [NSURL URLWithString:@"http://flv2.bn.netease.com/videolib3/1606/18/tQFLC2679/SD/tQFLC2679-mobile.mp4"];
//    //构造AVPlayerItem对象
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//    //初始化player
//    self.videoPlayer = [[AVPlayer alloc] initWithPlayerItem:item];
//    //到这里还没有画面,我们还需要一个AVPlayerLayer对象
//    self.layer1 = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
//    self.layer1.frame = CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height/3-10);
//    self.layer1.videoGravity = AVLayerVideoGravityResizeAspect;//让其居中显示
//    //添加到屏幕上显示
//    [self.view.layer addSublayer:self.layer1];
//    
//    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height/3-10)];
//    
//    [self.view addSubview:self.myView];
    
    
//    PlayeViewController *plaVc = [[PlayeViewController alloc] init];
//    videoModel *vMod = [self.myArray objectAtIndex: indexPath.row];
//    plaVc.model = vMod;
//    [self presentViewController:plaVc animated:YES completion:^{
//              
//    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
